package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.instance.entity.Attendance;
import com.a01_2021.thousandstimes.instance.mapper.AttendanceMapper;
import com.a01_2021.thousandstimes.instance.service.IAttendanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements IAttendanceService {

    @Resource
    AttendanceMapper attendanceMapper;

    /**
     * 签到
     *
     * @param userId 用户id
     * @return 签到实体列
     */
    @Override
    public Attendance checkIn(long userId) {
        Attendance attendance = new Attendance(null, userId, LocalDateTime.now(), 0);
        attendanceMapper.insert(attendance);
        return attendance;
    }

    /**
     * 签退
     *
     * @param userId 用户id
     * @return 签到实体列
     */
    @Override
    public Attendance checkOut(long userId) {
        Attendance attendance = new Attendance(null, userId, LocalDateTime.now(), 1);
        attendanceMapper.insert(attendance);
        return attendance;
    }

    /**
     * 获取用户签到签退记录
     *
     * @param userId 用户id
     * @return 签到记录列表
     */
    @Override
    public List<Attendance> getAttendance(long userId) {
        QueryWrapper<Attendance> attendanceQueryWrapper = new QueryWrapper<>();
        attendanceQueryWrapper.eq("user_id", userId);
        return attendanceMapper.selectList(attendanceQueryWrapper);
    }
}
